const { getInsures, getInsureById, addInsure, updateInsureById, delInsureById } = require('../dao/insuresDao')
const { moveFiles, removeFiles } = require('../utils/handleFiles');

//获取所有保险产品
module.exports.getInsures = async params => {
    console.log(params);
    const data = await getInsures(params);
    return {
        message: '获取保险产品成功',
        status: 1,
        data
    }
}

//根据ID保险产品
module.exports.getInsureById = async params => {
    const data = await getInsureById(params);
    if (data[0]._id) {
        return {
            message: '获取保险产品成功',
            status: 1,
            data
        }
    } else {
        return {
            message: '获取保险产品失败',
            status: 0,
            data
        }
    }
}

//新增保险产品
module.exports.addInsure = async params => {
    if (params.coverMap != '') {
        moveFiles({
            fromPath: './public/temp',
            toPath: './public/images',
            filename: params.coverMap
        });
    }
    if (params.coverDetails != '') {
        moveFiles({
            fromPath: './public/temp',
            toPath: './public/images',
            filename: params.coverDetails
        });
    }
    if (params.coverDetails != '' || params.coverMap != '') {
        removeFiles('./public/temp');
    }
    const data = await addInsure(params);
    if (data._id != "") {
        return {
            message: '新增保险产品成功',
            status: 1,
            data
        }
    } else {
        return {
            message: '新增保险产品失败',
            status: 0,
            data
        }
    }
}

//修改保险产品
module.exports.updateInsureById = async params => {
    const data = await updateInsureById(params);
    if (data.nModified > 0) {
        return {
            message: '保险产品修改成功',
            status: 1,
        }
    } else {
        return {
            message: '保险产品修改失败',
            status: 0
        }
    }
}

//删除保险产品
module.exports.delInsureById = async params => {
    const data = await delInsureById(params);
    if (data.nModified > 0) {
        return {
            message: '保险产品删除成功',
            status: 1
        };
    } else {
        return {
            message: '保险产品删除失败',
            status: 0
        };
    }
}